package com.question.dicegame;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DiceGameFrame extends JFrame {

	private MainScreen screen;
	private DiceGameBoard screen2;

	// 마우스를 눌렀을 때 위치를 저장하기 위한 변수
	private Point startPoint = null;
	private Point endPoint = null;

	private Clip clip;
	
	public DiceGameFrame() {
		init();
	}

	private void init() {

		setSize(new Dimension(Constants.FRAME_WIDTH, Constants.FRAME_HEIGHT));
		setResizable(false);
		setTitle("Dice Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		addMouseListener(screenMouseHandler);
		// 키보드 이벤트 처리기 등록
		addKeyListener(keyHandler);
		
		screen = new MainScreen();
		screen.setFocusable(true);
		screen.setPreferredSize(new Dimension(Constants.FRAME_WIDTH,
				Constants.FRAME_HEIGHT));

		add(screen);
		pack();
		validate();
	}

	// event handler
	// -> Frame의 필드 객체로
	// 익명 인터페이스를 구현한 클래스 또는
	// 인터페이스를 미리 구현한 어댑터 클래스를 상속한 클래스 사용
	private MouseAdapter screenMouseHandler = new MouseAdapter() {
		@Override
		public void mousePressed(MouseEvent e) {
			startPoint = e.getPoint();
			if (startPoint.x >= 360
					&& startPoint.x <= 600
					&& startPoint.y >= 640
					&& startPoint.y <= 726) {
			screen.click();
			repaint();
			}
			if (startPoint.x >= 680
					&& startPoint.x <= 920
					&& startPoint.y >= 640
					&& startPoint.y <= 726) {
			screen.click1();
			repaint();
			}
			
			if (startPoint.x >= 40
					&& startPoint.x <= 280
					&& startPoint.y >= 640
					&& startPoint.y <= 726) {
			screen.click2();
			repaint();
			}
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			endPoint = e.getPoint();

			if (startPoint.x >= 360
					&& startPoint.x <= 600
					&& startPoint.y >= 640
					&& startPoint.y <= 726
					&& endPoint.x >= 360
					&& endPoint.x <= 600
					&& endPoint.y >= 640
					&& endPoint.y <= 726) {
				screen2 = new DiceGameBoard();
				screen2.setFocusable(true);
				screen2.setPreferredSize(new Dimension(Constants.FRAME_WIDTH,
						Constants.FRAME_HEIGHT));
				add(screen2);
				remove(screen);
				removeMouseListener(screenMouseHandler);
				pack();
				validate();
				
				play("./bgm/dreams.wav");
			}
			
			if (startPoint.x >= 680
					&& startPoint.x <= 920
					&& startPoint.y >= 640
					&& startPoint.y <= 726
					&& endPoint.x >= 680
					&& endPoint.x <= 920
					&& endPoint.y >= 640
					&& endPoint.y <= 726) {
				
				String[] buttons = { "종료", "취소" };
				int result = JOptionPane.showOptionDialog(DiceGameFrame.this,
						 "종료 하시겠습니까?", "종료", JOptionPane.YES_NO_OPTION,
						JOptionPane.CANCEL_OPTION, null, buttons, "취소");
				System.out.println(result);
				switch (result) {
				case 0:
					System.exit(0);
					break;
				default:
					break;
				}
				
			}
			
			
			startPoint = null;
			endPoint = null;
			screen.clickEnd();
			repaint();
		}
	};

	private KeyAdapter keyHandler = new KeyAdapter() {

		@Override
		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_ESCAPE:
				System.out.println("@312321");
				// 게임종료
				String[] buttons = { "메인으로", "종료", "취소" };
				int result = JOptionPane.showOptionDialog(DiceGameFrame.this,
						"종료", "종료 하시겠습니까?", JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE, null, buttons, "취소");
				System.out.println(result);
				switch (result) {
				case 0:
					screen2.running = false;
										
					add(screen);
					remove(screen2);
					clip.stop();
					screen2.removeMouseListener(screenMouseHandler);
					addMouseListener(screenMouseHandler);
					repaint();
					break;
				case 1:
					System.exit(0);
					break;
				default:
					break;
				}
			}
			super.keyPressed(e);
		}
	};
	

	public void play(final String fileName) {
		try {
			clip = AudioSystem.getClip();
			AudioInputStream inputStream = AudioSystem
					.getAudioInputStream(new File(fileName));
			clip.open(inputStream);
			clip.start();
			
		} catch (Exception e) {
		}
	}
}
